Repository navigation
UI: TextureRenderCore's renderUnitAnimationWithBlendingUnk at 100% - #480
Merged
Merged
Conversation
TextureRenderCore::renderUnitAnimationWithBlendingUnk: blends a unit animation's TGX picture into the map or menu surface through the player's colour palette and a pair of rows of the blend filter arrays, picked by the blend strength. The four drawing loops (RGB555 and RGB565, zoomed out and not) are the original's handwritten assembly, as inline asm. From gynt's asm draft: his 91.5% 3c9bcdb ("Four asm loops recovered, stack slot allocation differs") on sourcehold's reimpl/TextureRenderCore, through our draft in sourcehold#363 (92.5%, 29ca840), which fixed his two zoomed-path blocks drawing through the picture width as the surface pointer. His newest version, 3d17527 (2026-11-02, with d90dce4's style pass on top) on his fork's reimpl/UI-MenuItems-development, is the decompiler's C form (6.0%, no asm); not used. What the match needed: - The zoomed path in an else block, with the column offset declared in it. The stack slots of the asm-referenced locals follow their reference weights, and every asm block adds to the weight of each local that any asm block writes, if that local is declared in a block that contains it (wherever in that block). At the top the column offset weighs more than the line width, the start height and the palette and takes their slots; in the zoomed block it falls behind them, as in the original (at the top: reccmp 95.8). - /GL with InitBlendFilterArraysUnk (100% on main): with its body visible the compiler keeps height in edi across the call, as the original does, and keeps the two surface arms' stores apart. - The map arm's stores with the heights last (with them first: 99.9), and the zoomed path's column parity first (after the heights: 98.8). Also: the header's parameter names, isZoom2 tested by truthiness, the TGX token constants and C hex in the asm, labels by what they do, no leading underscores, the filter rows as array indexing, declarations at their first assignment where both paths don't share them. Same code. Measured in a narrow build of this file with InitBlendFilterArraysUnk on main 0e4a83b, both resolver sets on: 100.0% raw and normalised. Not run in game. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Style guide reading (advisory, posted once) 38 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.
WhereRule 11 — No magic addresses
Rule 18 — Use the name, not the number
|
gynt
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One function at 100%, from our draft in #363 (92.5% there), which started from gynt's asm draft. No header changes. It needs
/GLwith its one callee, so the PR adds a/GLline and acmake/function-visibility.txtentry. One behaviour fix against his asm draft (below), already in #363.TextureRenderCore::renderUnitAnimationWithBlendingUnkgynt's asm draft is 3c9bcdb (2026-10-04, "reimplement: SHC_3BB0A8C1_0x00451E00 91.5% Four asm loops recovered, stack slot allocation differs") on sourcehold's reimpl/TextureRenderCore. Before it he had the decompiler's C form at 6.0% (cb5b9c1 / f9dd597, 2026-10-02 and 2026-10-03), which is what #255 had when it closed. His newest version is 3d17527 (2026-11-02, "reimplement: swap if/else arms across UI/Rendering, BuildingMenus and Helpers", with d90dce4's style pass on top) on his fork's reimpl/UI-MenuItems-development. It is the decompiler's C form again (6.0%, no asm), so it isn't used here. 8ca7861 on his fork's reimpl/Wilps-2026-10-18 has the asm draft with the bug below.
Measured
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed).InitBlendFilterArraysUnk.cpp(its one callee, 100% on main), on main 0e4a83b: 100.0 raw and normalised, with no diff line. The whole listing is the same on both sides: the original's 864 instructions, with every operand the same, the stack slots included. In that buildInitBlendFilterArraysUnkreads 97.9, as it does in a build of it alone without this file and in the full build below. That is reccmp's printing of its loop ends, addresses just past the table's end (below), not its code: the instructions are the same.isZoom2by truthiness, changes three comments and puts the four asm blocks between// clang-format offand// clang-format on, and narrow builds of the two give byte-identical listings), with both resolver sets on and the branch's own/GLlist (main's plus this file, next toInitBlendFilterArraysUnk.cpp, which main already lists), and with 0x004314E0 (theMapEditorLandscaping_GeneralButtonshandler) left out and its resolver off, as in main's baseline: 0 failed of 2,426 compile steps, 99.58 over 2,442 functions.renderUnitAnimationWithBlendingUnkreads 100.0 raw and normalised there, with no diff entry. Its calleeInitBlendFilterArraysUnkreads 97.9 (95.0 normalised), as on main, with the same two lines as there: its loop ends just past the table's end (0x00D814DA and 0x00D814DC), which reccmp names after the DLL symbol that lies there on our side. The other 48TextureRenderCorefunctions andApplyBlendingread exactly what they read before. Against main's own build (made at f489b72, 2,439 functions) the only other changes are main's since then (One more UI::MenuModals function at 100%: ProgressBarBox #467'sMenuModalRenderFunction_ProgressBarBoxand its constructor, UI: the single-player game launch at 100% #466'sLaunchSinglePlayerGameUnk), and every other function reads exactly what it read before (unrounded 99.5790 raw and 99.5529 normalised, 2,326 functions at 100.0 raw; main 0e4a83b alone: 99.5788 and 99.5528 over 2,441, 2,325 at 100.0). Thefunction-visibility.txtentry is used only by the wrapper-unit build (OPENSHC_FUNCTION_VISIBILITY_UNITS, off by default), so this/GLbuild does not exercise it.What the function does
It draws one frame of a unit animation, blended. The TGX picture's pixels are byte indices into the sprite's player colour palette. The unit's owner 1 and 4 are swapped first, and
colorPaletteOverridecan replace the palette. Each looked-up colour is blended over the destination through two rows ofDAT_BlendFilterArrays: rowblendStrengthUnkweights the destination and row32 - blendStrengthUnkthe drawn colour. A strength of 32 or more draws nothing. The four drawing loops (RGB555 and RGB565, zoomed out and not) are the original's handwritten assembly, as inline asm. The zoomed-out ones draw every second row and skip the odd columns, fromxPosition & 1on.Behaviour against the draft
widthin the two zoomed-out blocks. Only the other path setswidthto the surface pointer, so those blocks draw through the picture width as a pointer. Reimplement UI::Rendering TextureRenderCore's renderUnitAnimationWithBlendingUnk from gynt's asm draft (draft, below 100%) #363 fixed this already (29ca840).The rest does what the draft does.
What got it to 100%
Each of these was measured in a narrow build against the alternative it names:
elseblock, with the column offset declared inside it. This is what moved the stack slots. The original's order is the line width at -4, the start height at -8, the palette at -12, the column offset at -16, then the two filter rows and the column parity. With the column offset declared at the top, it took -4 and moved each of the other three down one slot. The compiler's slot order follows each local's reference weight (main'stools/compiler-internals/slot_probe.pyreads the weights from the compiler). An__asmblock adds to the weight of every local that any asm block in the function writes, as long as that local is declared in a block that contains the asm block, wherever in that block the declaration stands. The column offset is written only in the zoomed-out blocks, but declared at the top of the function it counted at all four blocks, so it weighed 10, against 7, 7 and 6 for the other three. In theelseblock it weighs 6 and comes after the palette, as in the original. Declared at the top in this code, it reads 95.8; Reimplement UI::Rendering TextureRenderCore's renderUnitAnimationWithBlendingUnk from gynt's asm draft (draft, below 100%) #363's file with only this change reads 96.0. Declaring it lower down at function level changes nothing (the probe reads 10 either way); only a block scope does./GLwithInitBlendFilterArraysUnk(on main's/GLlist already, with entries forrenderGmWithPreparedAlphaMaskandrenderInterfaceOrBuildingOccupationAreainfunction-visibility.txt). With the callee's body visible, the compiler keepsheightin edi across the call as the original does (0x00451E3E to 0x00451EEB), and keeps the two surface arms' stores apart (0x00451EBD and 0x00451ED9). Without the/GLline this code reads 96.1 (71.2 normalised):heightis loaded again behind the call, andblendStrengthUnkis in edi instead of ebx.andand its store move down, and three registers swap roles (0x00452340 to 0x0045236C).Changes against #363's code (same code)
xPosition,yPosition,imageAddress), andisZoom2is tested by truthiness, as everywhere on main.TT_*constants (TgxToken.hpp), as main'srenderGmWithPreparedAlphaMaskdoes. The colour masks are written as C hex and the shift counts in decimal.skip_token_555,draw_row_zoomed_565, ...) instead ofa5skip,b6row.lineByteWidthis nowlineBytes, as inrenderGmWithPreparedAlphaMask. The palette and the two filter rows are declared where they are first assigned, and the filter rows are written as array indexing (DAT_BlendFilterArrays::instance[blendStrengthUnk][0]) instead of byte offsets.__asmblock sits between// clang-format offand// clang-format on, as in main's other TextureRenderCore asm files.returnbefore theelse. The draft's 15-lineNOTEcomment is cut to one line; what it said is in this text.Why the loops are inline assembly
Every token read starts with
mov ecx, 0before a byte load intocl, and no compiler materialises zero that way. The blocks compare withcmp reg, 0instead oftest, load the same token byte into two registers, and usemulfor the row offset. Each block's start (the x test, the clipping against the start height) has the same tells (cmp eax, 0), so it stays in the block. The surface choice, the clamp of the height and the filter rows are C++.Checker CHECKs (not changed)
0xffe0,0xfc1f,0x3ff,0x83ff,0xf81f,0x7ff), and0xfffffffe, which rounds xPosition down to an even column on the zoomed-out surface. Main'srenderGmWithPreparedAlphaMask,renderInterfaceOrBuildingOccupationAreaandApplyBlendingwrite these masks the same way (Reimplement 2 UI::Rendering TextureRenderCore asm functions from gynt's drafts (100%) #389 went in with 50 such findings). In decimal the bit layout would be lost.lineBytes,heightStartandheightEndare assigned in both paths and in both surface arms and read after them, so they are declared before theif; the asm reads the first two from their stack slots.columnOffsetis written only by the asm.cmake/function-visibility.txt: the entry for this function's callee under/GL, betweenrenderGmWithPreparedAlphaMaskandtransformTilesetToRGB565by address.Proposed, not changed here
TextureRenderCore::colorPaletteOverrideis adwordin main's header. This function reads it as a palette (short*), so the code casts it. Proposed:short*. Not changed: on main only the constructor writes it (0), and its other writers in the exe aren't reimplemented yet.blendStrengthUnkis the blend strength from 0 to 31. From the surface choice on, the function reuses it as the line step the asm adds per row, as the original does (0x00451EBD). Proposed name:blendStrength.VC_MAP_SURFACE_WIDTH = 4056would name the map surface's width here and on the 35 lines of main's.cppfiles that write 4056.Other PRs
/GLline sits with the other TextureRenderCore lines, not at the end of the list, so it doesn't conflict with the 16 open PRs that add lines at the end. Checked withgit merge-treeagainst all 100 open PRs: every other one merges with this one exactly as with main,cmake/function-visibility.txtincluded.🤖 Generated with Claude Code